package pers.jsan.litheopen.security.filter.login;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import pers.jsan.litheopen.security.exception.LitheAuthenticationException;
import pers.jsan.litheopen.security.filter.AuthenticationFilterChain;
import pers.jsan.litheopen.security.filter.LitheAuthenticationFilter;
import pers.jsan.litheopen.security.filter.RequestWrapper;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class CheckLoginNameAndPasswordFilter implements LitheAuthenticationFilter {

    @Override
    public void doFilter(HttpServletRequest servletRequest, HttpServletResponse servletResponse, AuthenticationFilterChain filterChain) throws IOException, ServletException, LitheAuthenticationException {
        RequestWrapper requestWrapper = new RequestWrapper(servletRequest);
        String str = requestWrapper.getBodyString();
        JSONObject jsonObject = JSONObject.parseObject(str);
        String loginName = jsonObject.getString("loginName");
        String password = jsonObject.getString("password");
        if(StringUtils.isBlank(loginName)) {
            throw new LitheAuthenticationException("账号错误");
        }
        if(StringUtils.isBlank(password)) {
            throw new LitheAuthenticationException("密码错误");
        }
        filterChain.doFilter(servletRequest, servletResponse);
    }
}
